<template>
    <div>
        <h2>会员信息添加</h2>
        <table class="table table-primary">
            <tbody>
                <tr>
                    <td>会员名称</td>
                    <td>
                        <input type="text" v-model="quertinfo.Name">
                    </td>
                </tr>

                <tr>
                    <td>性别</td>
                    <td>
                        <input type="radio" name="sex" :value="0" v-model="quertinfo.Sex">男
                        <input type="radio" name="sex" :value="1" v-model="quertinfo.Sex">女
                    </td>
                </tr>

                <tr>
                    <td>手机号码</td>
                    <td>
                        <input type="text" v-model="quertinfo.Phone">
                    </td>
                </tr>

                <tr>
                    <td>会员级别</td>
                    <td>
                        <select v-model="quertinfo.Lid">
                            <option value="">请选择</option>
                            <option :value="item.Id" v-for="item in quertinfo1.data" :key="item.Id">{{item.Name}}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td>积分</td>
                    <td>
                        <input type="text" v-model="quertinfo.JiFen">
                    </td>
                </tr>

                <tr>
                    <td>累计消费金额</td>
                    <td>
                        <input type="text" v-model="quertinfo.Money">
                    </td>
                </tr>

                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="添加" class="btn btn-success" @click="ok">
                        <input type="button" value="取消" class="btn btn-success" @click="cel">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter();
import moment from 'moment';

//下拉框
//生命周期函数
onMounted(() => {
    showlevel();
})

let quertinfo1: any = reactive({
    data: []
})

const showlevel = () => {
    axios({
        method: 'get',
        url: '/api/Week/Showlevel'
    })
        .then(res => {
            console.log(res);
            quertinfo1.data = res.data
        })
        .catch(err => {
            console.error(err);
        })
}

let quertinfo =reactive({
  "Name": "",
  "Sex": 0,
  "Phone": "",
  "Lid": '',
  "JiFen": '',
  "Money": '',
})

const ok=()=>{
//判断有效性
if(quertinfo.Name==""){
    alert('会员名称不能为空');
    return;
}

if(quertinfo.Phone==""){
    alert('手机号码不能为空');
    return;
}
let rr=/^1[3-9]\d{9}$/g;
if(!rr.test(quertinfo.Phone)){
    alert('手机号格式不对');
    return;
}

if(quertinfo.Lid==""){
    alert('会员级别不能为空');
    return;
}

if(quertinfo.JiFen==""){
    alert('积分不能为空');
    return;
}
let leg=/^\d+$/g;
if(!leg.test(quertinfo.JiFen)){
    alert('积分必须是整数');
    return;
}

if(quertinfo.Money==""){
    alert('累计消费金额不能为空');
    return;
}


let reg=/^\d+$/g;
if(!reg.test(quertinfo.Money)){
    alert('累计消费金额必须是整数');
    return;
}

axios({
    method:'post',
    url:'/api/Week/AddInfo',
    data:quertinfo
})
.then(res=>{
    console.log(res);
    if(res.data>0){
        alert('成功');
        router.push('/infoshow')
    }
    else{
        alert('失败')
    }
})

}

const cel=()=>{
    router.push('/infoadd')
}
</script>

<style scoped>

</style>